package com.gbits.app.finance.controller;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gbits.app.finance.api.Response;
import com.gbits.app.finance.convert.DefaultSubjectConvert;

@Controller
@RequestMapping("/subject")
public class SubjectController {

	private static Logger LOG = LoggerFactory.getLogger(SubjectController.class);
	@Autowired
	private DefaultSubjectConvert defaultSubjectConvert;
	@RequestMapping("/info")
	@ResponseBody
	public Response<Object> info(final @RequestBody Map<String,Object > params, HttpServletRequest req, HttpServletResponse resp) {
		
		try {
			String str = this.defaultSubjectConvert.doConvert(params);
			return new Response(true, "success", str);
		} catch (Exception e) {
			LOG.error(e.getMessage(), e);
			
			return new Response(false, e.getMessage(), "");
		}
	}
	
	Map<String, Object> convert(final @RequestBody Map<String,Object > params, HttpServletRequest req, HttpServletResponse resp) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cost_organ_name", params.get("cost_organ_name"));
		map.put("organ_name", params.get("organ_name"));
		map.put("pay_bank_no", params.get("pay_bank_no"));
		map.put("amount", params.get("amount"));
		map.put("company_str", params.get("company_str"));
		map.put("account_id", params.get("account_id"));
		
		List<Map<String, Object>> subjectList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> taxList = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> payList = new ArrayList<Map<String, Object>>();
		
		map.put("pay_list", payList);
		map.put("tax_list", taxList);
		map.put("subject_list", subjectList);
		
		for (int i=0; i<10; i++) {
			String name = req.getParameter("cost_organ_name" + i);
			if (null == name) {
				break;
			}
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("cost_organ_name", req.getParameter("cost_organ_name" + i));
			sub.put("organ_name", req.getParameter("organ_name" + i));
			sub.put("subject_name", req.getParameter("subject_name" + i));
			sub.put("amount", req.getParameter("amount" + i));
			sub.put("nusage", req.getParameter("nusage" + i));
			subjectList.add(sub);
		}
		for (int i=0; i<10; i++) {
			String name = req.getParameter("tax_amount" + i);
			if (null == name) {
				break;
			}
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("tax_amount", req.getParameter("tax_amount" + i));
			sub.put("no_tax_amount", req.getParameter("no_tax_amount" + i));
			sub.put("is_contain_tax", req.getParameter("is_contain_tax" + i));
			taxList.add(sub);
		}
		for (int i=0; i<10; i++) {
			String name = req.getParameter("account_name" + i);
			if (null == name) {
				break;
			}
			Map<String, Object> sub = new HashMap<String, Object>();
			sub.put("account_name", req.getParameter("account_name" + i));
			sub.put("amount", req.getParameter("amount" + i));
			sub.put("is_company", req.getParameter("is_company" + i));
			payList.add(sub);
		}
		
		return map;
	}
	
	Map<String, Object> getParameter(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		Enumeration<String> names = req.getParameterNames();
		while (names.hasMoreElements()) {
			String name = names.nextElement();
			Object value = req.getParameter(name);
			if (!StringUtils.isEmpty(value)) {
				map.put(name, value);
			}
		}
		return map;
	}
}
